package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.Set;

@Log4j2
@Slf4j
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品:{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        String key = "dish_" + dishDTO.getCategoryId();
        extracted(key);

        return Result.success(dishDTO);
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> getDishList(DishPageQueryDTO dishPageQueryDTO) {
        log.info("查询菜品:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.getDishList(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("起售停售")
    public Result updateStatus(@PathVariable("status") Integer status, @RequestParam Long id) {
        dishService.updateDishStatus(status, id);
        return Result.success(null);
    }


    @DeleteMapping()
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品ID:{}", ids);
        dishService.deleteBatch(ids);

        extracted("dish_*");
        return Result.success(null);
    }

    private void extracted(String pattern) {
        //删改查操作后删除redis缓存
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }

    @PutMapping()
    @ApiOperation("更新菜品")
    public Result updateDish(@RequestBody DishDTO dishDTO) {
        log.info("更新菜品：{}", dishDTO);
        dishService.updateDisWithFlavor(dishDTO);
        extracted("dish_*");
        return Result.success(null);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result updateDish(@PathVariable("id") Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVOList = dishService.getByDishId(id);
        return Result.success(dishVOList);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result getDishList(Long categoryId) {
        log.info("根据id查询菜品：{}", categoryId);
        List<Dish> list = dishService.getByDishList(categoryId);
        return Result.success(list);
    }
}
